#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#include <ual_types.h>
#include <port_conf.h>
#include <net_conf.h>
#include <srv_conf.h>
#include <psn_conf.h>

#include "tools_utils.h"
#include <device_i2ceeprom.h>

#include <rtfs_build_ver.h>

/*颜色接口函数*/
#include "../000.common/000.common.h"

/*使用中文打印提示信息*/
int g_lang_zhCN=0;

/*传参是否开启调试信息*/
int g_debug_show=0;

#define BUF_SIZE_1024  1024
#define BUF_SIZE_256   256
#define BUF_SIZE_128   128
#define BUF_SIZE_64    64
#define BUF_SIZE_32    32

//格式为xxx.xxx.xxx.xxx的IP地址长度
#define IP_BUF_SIZE 16

#define TRUE  1
#define FALSE 0

//网络类型位置
#define NET_CLASS_DIR  "/sys/class/net"

//用户抽象层位置
#define UAL_TOP_DIR "/ual"

//MTD目录名称
#define UAL_MTD_DIR "mtdinfo"

//配置数据位置
#define CFG_TOP_DIR "/root"

#ifndef APP_ELF_NAME
	//应用程序名称
	#define APP_ELF_NAME "sysapp"
#endif

#ifndef APP_TOP_DIR
	//应用程序位置
	#define APP_TOP_DIR "/root"
#endif

typedef struct netcfg_t
{
	char name[IP_BUF_SIZE];
	char proto[IP_BUF_SIZE];
	char ipaddr[IP_BUF_SIZE];
	char netmask[IP_BUF_SIZE];
	char gateway[IP_BUF_SIZE];
} netcfg;

netcfg g_netcfg_grp[2];

int get_node_ethidx(ual_net_node *node)
{
	if(0 == strcmp(node->name, "eth0"))
	{
		return 0;
	}
	else if(0 == strcmp(node->name, "eth1"))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int read_ret_string(FILE *pfp)
{
	char buf[BUF_SIZE_1024];
	while(fgets(buf, BUF_SIZE_1024, pfp) != NULL)
	{
		fprintf(stdout, "%s", buf);
	}
}

int read_ret_buf(FILE *pfp, char *retbuf, int buflen)
{
	int cnt = 0;
	while(fgets(retbuf, buflen, pfp) != NULL)
	{
		if(cnt > 1)
		{
			if(g_lang_zhCN)
			{
				serro("读取返回值内容超过缓存区大小:%d", buflen);
			}else
			{
				serro("read ret buf out of retbuf size:%d", buflen);
			}
			return -1;
		}
		cnt++;
	}
	return 0;
}

#define debug 1

#define dtags "rc.local"

#if (debug)
	#define dmsg(fmt, args...) \
	do \
	{  \
		if(g_debug_show) { sshow(fmt, ##args); } \
	} while(0)
#else
	#define dmsg(fmt, args...)
#endif

#define BINCMD_CLR_REG  0
#define BINCMD_SET_PSN  1
#define BINCMD_GET_PSN  2

#define CMD_HEAD_HEX  0xE9
#define CMD_TAIL_HEX  0x9E

#define CMD_IDX_HEAD 0
#define CMD_IDX_ID   1
#define CMD_IDX_LEN  2
#define CMD_IDX_DATA 3

#define CMD_LEN_HEAD 3
#define CMD_LEN_MIN  5
#define CMD_LEN_SUM  1
#define CMD_LEN_TAIL 1

/*psn offset and char total*/
#define DATA_PSN_OFFSET 0
#define BINDAT_PSN_LENGTH 16

#define PINFO_ALIAS_LEN 8
#define PINFO_ALIAS_IDX 0
#define PINFO_YEAR_LEN 2
#define PINFO_YEAR_IDX ((PINFO_ALIAS_IDX)+(PINFO_ALIAS_LEN))
#define PINFO_MONTH_LEN 2
#define PINFO_MONTH_IDX ((PINFO_YEAR_IDX)+(PINFO_YEAR_LEN))
#define PINFO_DBSNH_LEN 2
#define PINFO_DBSNH_IDX ((PINFO_MONTH_IDX)+(PINFO_MONTH_LEN))
#define PINFO_DBSNL_LEN 2
#define PINFO_DBSNL_IDX ((PINFO_DBSNH_IDX)+(PINFO_DBSNH_LEN))

#define MAC_STR_LENGTH  17
#define MAC_DEF_PMACHD  "BC"
#define MAC_ERR_SENDCMD 0xFD
#define MAC_ERR_READCMD 0xFE
#define MAC_ERR_PSNTYPE 0xFF
#define MAC_ERR_DEFADDR1 "01"
#define MAC_ERR_DEFADDR2 "02"


#if defined (I2C_SYSROM_PATH)

void fill_sysbin_cmd_common(int cmdid, int cmdlen, char *cmdbuf)
{
	cmdbuf[CMD_IDX_HEAD]  = CMD_HEAD_HEX;
	cmdbuf[CMD_IDX_ID]    = cmdid;
	cmdbuf[CMD_IDX_LEN]   = cmdlen;
	cmdbuf[cmdlen-2]      = 0;
	cmdbuf[cmdlen-1]      = CMD_TAIL_HEX;
}

int create_sysbin_cmd_package(int cmdid, char *cmdbuf, char *databuf, int len)
{
	int cmdlen  = 0;
	int dataidx = 0;

	switch(cmdid)
	{
		case BINCMD_SET_PSN:
			{
				if(len != BINDAT_PSN_LENGTH)
				{
					if(g_lang_zhCN)
					{
						serro("产品序列号长度错误，当前长度:%d, 要求长度:%d [ERROR]", len, BINDAT_PSN_LENGTH);
					}else
					{
						serro("Error psn length: %d, need: %d [ERROR]", len, BINDAT_PSN_LENGTH);
					}
					return -1;
				}
				cmdlen = CMD_LEN_HEAD+BINDAT_PSN_LENGTH+CMD_LEN_SUM+CMD_LEN_TAIL;

				fill_sysbin_cmd_common(cmdid, cmdlen, cmdbuf);
				
				for(dataidx=0; dataidx < BINDAT_PSN_LENGTH; dataidx++)
				{
					cmdbuf[CMD_IDX_DATA+dataidx] = databuf[dataidx];
				}
				return cmdlen;
				break;
			}
		case BINCMD_GET_PSN:
			{
				cmdlen = CMD_LEN_HEAD+CMD_LEN_SUM+CMD_LEN_TAIL;
				fill_sysbin_cmd_common(cmdid, cmdlen, cmdbuf);
				return cmdlen;
				break;
			}
		case BINCMD_CLR_REG:
			{
				cmdlen = CMD_LEN_HEAD+CMD_LEN_SUM+CMD_LEN_TAIL;
				fill_sysbin_cmd_common(cmdid, cmdlen, cmdbuf);
				return cmdlen;
				break;
			}
		default:
			{
				if(g_lang_zhCN)
				{
					serro("未知的命令类型 [ERROR]");
				}else
				{
					serro("Unkown cmd type [ERROR]");
				}
				return -1;
			}
	}
	return -1;
}

int write_sysbin(char *buf, int len)
{
	int   ret  = -1;

	int  sysbin_fd = -1;

    sysbin_fd = open(I2C_SYSROM_PATH, O_RDWR);
    if (sysbin_fd < 0) 
	{
		if(g_lang_zhCN)
		{
			serro("打开sysbin失败 [ERROR]");
		}else
		{
			serro("Open sysbin failed [ERROR]");
		}
		return -1;
	}

    ret = write(sysbin_fd, buf, len);
    if(ret != len) 
	{
		if(g_lang_zhCN)
		{
			serro("写入sysbin数据失败 [ERROR]");
		}else
		{
			serro("Write sysbin data failed [ERROR]");
		}
		close(sysbin_fd);
		return -1;
	}
	close(sysbin_fd);
	return 0;
}

int read_sysbin(char *buf, int len)
{
	int   ret  = -1;
	int   rlen = 0;

	int  sysbin_fd = -1;

    sysbin_fd = open(I2C_SYSROM_PATH, O_RDWR);
    if (sysbin_fd < 0) 
	{
		if(g_lang_zhCN)
		{
			serro("打开sysbin失败 [ERROR]");
		}else
		{
			serro("Open sysbin failed [ERROR]");
		}
		return -1;
	}

    ret = read(sysbin_fd, buf, len);
    if(ret != len) 
	{
		if(g_lang_zhCN)
		{
			serro("读取sysbin数据失败 [ERROR]");
		}else
		{
			serro("Read sysbin data failed [ERROR]");
		}
		close(sysbin_fd);
		return -1;
	}
	close(sysbin_fd);
	return ret;
}

int send_sysbin_cmd(int cmdid, char *databuf, int len)
{
	int   cmdlen = 0;
	char  cmdbuf[BUF_SIZE_128] = {0};

	cmdlen = create_sysbin_cmd_package(cmdid, cmdbuf, databuf, len);

	if(cmdlen <= 0)
	{
		if(g_lang_zhCN)
		{
			serro("创建sysbin命令包失败 [ERROR]");
		}else
		{
			serro("Create sysbin cmd package failed [ERROR]");
		}
		return -1;
	}
	if(0 != write_sysbin(cmdbuf, cmdlen))
	{
		if(g_lang_zhCN)
		{
			serro("写入sysbin命令包失败 [ERROR]");
		}else
		{
			serro("Write sysbin cmd package failed [ERROR]");
		}
		return -1;
	}
	return 0;
}

int send_sysbin_clrcmd(void)
{
	if(0 != send_sysbin_cmd(BINCMD_CLR_REG, NULL, 0))
	{
		if(g_lang_zhCN)
		{
			serro("写入sysbin命令clrcmd包失败 [ERROR]");
		}else
		{
			serro("Write sysbin clrcmd failed [ERROR]");
		}
		return -1;
	}
	return 0;
}
#endif

int find_pinfo_pidx(char *alias)
{
	int pidx = 0;
	int pmax = sizeof(psnconf_list)/sizeof(psnconf_list[0]);
	for(pidx = 0; pidx < pmax; pidx++)
	{
		if(0 == strcmp(alias, psnconf_list[pidx].alias))
		{
			break;
		}
	}
	if(pidx >= pmax)
	{
		return -1;
	}
	return pidx;
}

int set_mac_default(char* ethname, unsigned char errno)
{
	char  wbuf[BUF_SIZE_128] = {0};
	char  macstr[MAC_STR_LENGTH+1] = {0};

	/*目前只支持最多2个以太网口*/
	if(0 == strcmp(ethname, "eth0"))
	{
		sprintf(macstr, "%s:%02X:EE:EE:00:%s", MAC_DEF_PMACHD, errno, MAC_ERR_DEFADDR1);
	}
	else if(0 == strcmp(ethname, "eth1"))
	{
		sprintf(macstr, "%s:%02X:EE:EE:00:%s", MAC_DEF_PMACHD, errno, MAC_ERR_DEFADDR2);
	}
	else
	{
		return -1;
	}
	sprintf(wbuf, "ifconfig %s hw ether %s", ethname, macstr);
	system(wbuf);

	if(g_lang_zhCN)
	{
		sinfo("网口%s的网卡地址(macaddr): %s(default)", ethname, macstr);
	}else
	{
		sinfo("%s macaddr: %s(default)", ethname, macstr);
	}
	return 0;
}

#if defined (I2C_SYSROM_PATH)
int setup_psn()
{
	int   rlen = 0;
	int   pidx = -1;
	char  wbuf[BUF_SIZE_128] = {0};
	char  rbuf[BUF_SIZE_128] = {0};

	char  macstr[MAC_STR_LENGTH+1] = {0};

	char  aliasstr[PINFO_ALIAS_LEN+1] = {0};

	if(0 != send_sysbin_cmd(BINCMD_GET_PSN, NULL, 0))
	{
		if(g_lang_zhCN)
		{
			serro("读取产品序列号(PSN): 请求失败");
		}else
		{
			serro("Read PSN: (request failed)");
		}
		return -1;
	}

	rlen = read_sysbin(rbuf, BINDAT_PSN_LENGTH);
	if(rlen != BINDAT_PSN_LENGTH)
	{
		send_sysbin_clrcmd();
		if(g_lang_zhCN)
		{
			serro("读取产品序列号(PSN): 读取失败");
		}else
		{
			serro("Read PSN: (read failed)");
		}
		return -1;
	}
	send_sysbin_clrcmd();

	strncpy(aliasstr, rbuf+PINFO_ALIAS_IDX, PINFO_ALIAS_LEN);
	pidx = find_pinfo_pidx(aliasstr);
	if(pidx < 0)
	{
		if(g_lang_zhCN)
		{
			serro("读取产品序列号(PSN): 未知产品");
		}else
		{
			serro("Read PSN: (unknown product)");
		}
		return -1;
	}
	else
	{
		if(g_lang_zhCN)
		{
			sinfo("读取产品序列号(PSN):	%s (%s)", rbuf, psnconf_list[pidx].pname);
		}else
		{
			sinfo("Read PSN: %s (%s)", rbuf, psnconf_list[pidx].pname);
		}

		sprintf(wbuf,"echo %s > %s/PSN", rbuf, UAL_TOP_DIR);
		system(wbuf);
	}
	return 0;
}
#endif

int setup_sysver()
{
	char  wbuf[BUF_SIZE_128] = {0};

	/*create rootfs version info*/
	sprintf(wbuf,"echo %s > /ual/ver_rootfs", RTFS_BUILD_VER_STR);
	system(wbuf);

	/*create kernel version info*/
	sprintf(wbuf,"ln -s /proc/ver_rawkern /ual/ver_rawkern");
	system(wbuf);

	/*create uboot version info*/
	sprintf(wbuf,"ln -s /proc/ver_rawboot /ual/ver_rawboot");
	system(wbuf);
}

#if defined (I2C_SYSROM_PATH)
int set_mac_from_psn(char *ethname, int ethoffset)
{
	int   rlen = 0;
	int   pidx = -1;
	
	char  rbuf[BUF_SIZE_128] = {0};

	char  pyear[PINFO_YEAR_LEN+1]  = {0};
	char  pmonth[PINFO_MONTH_LEN+1]= {0};
	char  pdbsnH[PINFO_DBSNH_LEN+1]= {0};
	char  pdbsnL[PINFO_DBSNL_LEN+1]= {0};

	unsigned char pcode = 0;

	char  macstr[MAC_STR_LENGTH+1] = {0};

	char  aliasstr[PINFO_ALIAS_LEN+1] = {0};

	if(0 != send_sysbin_cmd(BINCMD_GET_PSN, NULL, 0))
	{
		return MAC_ERR_SENDCMD;
	}

	rlen = read_sysbin(rbuf, BINDAT_PSN_LENGTH);
	if(rlen != BINDAT_PSN_LENGTH)
	{
		send_sysbin_clrcmd();
		return MAC_ERR_READCMD;
	}
	send_sysbin_clrcmd();

	strncpy(aliasstr, rbuf+PINFO_ALIAS_IDX, PINFO_ALIAS_LEN);
	pidx = find_pinfo_pidx(aliasstr);
	if(pidx < 0)
	{
		return MAC_ERR_PSNTYPE;
	}

	strncpy(pyear,  rbuf+PINFO_YEAR_IDX,  PINFO_YEAR_LEN);
	strncpy(pmonth, rbuf+PINFO_MONTH_IDX, PINFO_MONTH_LEN);
	strncpy(pdbsnH, rbuf+PINFO_DBSNH_IDX, PINFO_DBSNH_LEN);
	strncpy(pdbsnL, rbuf+PINFO_DBSNL_IDX, PINFO_DBSNL_LEN);

	pcode = psnconf_list[pidx].pcode + ethoffset;

	sprintf(macstr, "%s:%02x:%s:%s:%s:%s",
			psnconf_list[pidx].pmachd,
			pcode,
			pyear,
			pmonth,
			pdbsnH,
			pdbsnL);
	utils_set_macinfo(ethname, macstr);

	if(g_lang_zhCN)
	{
		sinfo("网口%s的网卡地址(macaddr): %s(psn)", ethname, macstr);
	}else
	{
		sinfo("%s macaddr: %s(psn)", ethname, macstr);
	}
	return 0;
}
#endif

/*end of eeprom-psn-mac code*/

int exec_cmd_only(char * cmdstr)
{
	FILE *pfp = NULL;
	pfp = popen(cmdstr,"r");
	if(!pfp)
	{
		if(g_lang_zhCN)
		{
			serro("执行命令失败: %s", cmdstr);
		}else
		{
			serro("Error exec cmd: %s", cmdstr);
		}
		return -1;
	}
	pclose(pfp);
	return 0;
}

int exec_cmd_getret(char * cmdstr, char *retbuf, int buflen)
{
	int ret = 0;
	FILE *pfp = NULL;
	pfp = popen(cmdstr,"r");
	if(!pfp)
	{
		if(g_lang_zhCN)
		{
			serro("执行命令失败: %s", cmdstr);
		}else
		{
			serro("Error exec cmd: %s", cmdstr);
		}
		return -1;
	}
	ret = read_ret_buf(pfp, retbuf, buflen);
	pclose(pfp);

	return ret;
}

int ubifs_mnt(int mtdidx, int ubiidx, char *mntdir, int needsync)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};

	if(g_debug_show)
	{
		dmsg("umount -l %s", mntdir);
		sprintf(cmdbuf,"umount -l %s > /dev/null 2>&1", mntdir);
		system(cmdbuf);

		dmsg("ubidetach /dev/ubi_ctrl -d %d", ubiidx);
		sprintf(cmdbuf,"ubidetach /dev/ubi_ctrl -d %d ", ubiidx);
		system(cmdbuf);

		dmsg("ubiattach /dev/ubi_ctrl -m %d -d %d", mtdidx, ubiidx);
		sprintf(cmdbuf,"ubiattach /dev/ubi_ctrl -m %d -d %d ", mtdidx, ubiidx);
		system(cmdbuf);
	}else
	{
		sprintf(cmdbuf,"umount -l %s >/dev/null 2>&1", mntdir);
		system(cmdbuf);
		sprintf(cmdbuf,"ubidetach /dev/ubi_ctrl -d %d >/dev/null 2>&1", ubiidx);
		system(cmdbuf);
		sprintf(cmdbuf,"ubiattach /dev/ubi_ctrl -m %d -d %d >/dev/null 2>&1", mtdidx, ubiidx);
		system(cmdbuf);
	}

	if(g_debug_show)
	{
		dmsg("mount -t ubifs /dev/ubi%d_0 %s && echo execdone", ubiidx, mntdir);
		sprintf(cmdbuf,"mount -t ubifs ubi%d_0 %s && echo execdone", ubiidx, mntdir);
	}else
	{
		if(0 != needsync)
		{
			sprintf(cmdbuf,"mount -t ubifs -o sync ubi%d_0 %s >/dev/null 2>&1 && echo execdone", ubiidx, mntdir);
		}else
		{
			sprintf(cmdbuf,"mount -t ubifs ubi%d_0 %s >/dev/null 2>&1 && echo execdone", ubiidx, mntdir);
		}
	}

	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		sprintf(cmdbuf,"ubidetach /dev/ubi_ctrl -d %d >/dev/null 2>&1", ubiidx);
		system(cmdbuf);

		if(g_lang_zhCN)
		{
			serro("尝试将ubi%d_0以ubifs格式挂载到目录%s执行失败 [ERROR]", ubiidx, mntdir);
		}else
		{
			serro("Try mount -t ubifs ubi%d_0 %s execute failed [ERROR]", ubiidx, mntdir);
		}
		return -1;
	}

	if(strstr(retbuf,"execdone") == NULL)
	{
		if(g_lang_zhCN)
		{
			serro("尝试将ubi%d_0以ubifs格式挂载到目录%s返回失败 [ERROR]", ubiidx, mntdir);
		}else
		{
			serro("Try mount -t ubifs ubi%d_0 %s return failed [ERROR]", ubiidx, mntdir);
		}
		ret = -1;
	}else
	{
		ret = 0;
	}
	return ret;
}

int ubifs_fix(int mtdidx, int ubiidx, char *volname)
{
	char cmdbuf[BUF_SIZE_128]={0};

	if(g_lang_zhCN)
	{
		sinfo("修复ubi%d对应的MTD分区mtd%d [START]", ubiidx, mtdidx);
	}else
	{
		sinfo("Fix ubi%d (mtd%d) partition [START]", ubiidx, mtdidx);
	}

	sprintf(cmdbuf,"ubidetach /dev/ubi_ctrl -d %d >/dev/null 2>&1", ubiidx);
	system(cmdbuf);
	
	sprintf(cmdbuf,"flash_erase /dev/mtd%d 0 0 >/dev/null 2>&1", mtdidx);
	system(cmdbuf);
	
	sprintf(cmdbuf,"ubiformat /dev/mtd%d >/dev/null 2>&1", mtdidx);
	system(cmdbuf);

	sprintf(cmdbuf,"ubiattach /dev/ubi_ctrl -m %d -d %d  >/dev/null 2>&1", mtdidx, ubiidx);
	system(cmdbuf);

	sprintf(cmdbuf,"ubimkvol /dev/ubi%d -N %s -m  >/dev/null 2>&1", ubiidx, volname);
	system(cmdbuf);

	if(g_lang_zhCN)
	{
		sinfo("修复ubi%d对应的MTD分区mtd%d [OK]", ubiidx, mtdidx);
	}else
	{
		sinfo("Fix ubi%d (mtd%d) partition [OK]", ubiidx, mtdidx);
	}
	return 0;
}

int yaffs2_mnt(int mtdidx, char *mntdir)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};

	//先尝试卸
	sprintf(cmdbuf,"umount -l %s >/dev/null 2>&1", mntdir);
	system(cmdbuf);

	//再尝试挂载
	sprintf(cmdbuf,"mount -t yaffs2 -o inband-tags /dev/mtdblock%d %s >/dev/null 2>&1 && echo execdone", mtdidx, mntdir);
	//sprintf(cmdbuf,"mount /dev/mtdblock%d %s >/dev/null 2>&1 && echo execdone", mtdidx, mntdir);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		//出现异常错误
		if(g_lang_zhCN)
		{
			serro("尝试将MTD分区mtd%d以yaffs2格式挂载到目录%s执行失败 [ERROR]", mtdidx, mntdir);
		}else
		{
			serro("Try mount yaffs2 mtd%d to %s execute failed [ERROR]", mtdidx, mntdir);
		}
		return -1;
	}

	if(strstr(retbuf,"execdone") == NULL)
	{
		//挂载命令执行失败
		if(g_lang_zhCN)
		{
			serro("尝试将MTD分区mtd%d以yaffs2格式挂载到目录%s返回失败 [ERROR]", mtdidx, mntdir);
		}else
		{
			serro("Try mount yaffs2 mtd%d to %s return failed [ERROR]", mtdidx, mntdir);
		}
		ret = -1;
	}else
	{
		//挂载命令执行成功
		ret = 0;
	}
	return ret;
}

int jffs2_mnt(int mtdidx, char *mntdir)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};

	//先尝试卸载
	sprintf(cmdbuf,"umount -l %s >/dev/null 2>&1", mntdir);
	system(cmdbuf);

	//再尝试挂载
	sprintf(cmdbuf,"mount -t jffs2 /dev/mtdblock%d %s >/dev/null 2>&1 && echo execdone", mtdidx, mntdir);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		//出现异常错误
		if(g_lang_zhCN)
		{
			serro("尝试将MTD分区mtd%d以jffs2格式挂载到目录%s执行失败 [ERROR]", mtdidx, mntdir);
		}else
		{
			serro("Try mount jffs2 mtd%d to %s execute failed [ERROR]", mtdidx, mntdir);
		}
		return -1;
	}
	if(strstr(retbuf,"execdone") == NULL)
	{
		//挂载命令执行失败
		if(g_lang_zhCN)
		{
			serro("尝试将MTD分区mtd%d以jffs2格式挂载到目录%s返回失败 [ERROR]", mtdidx, mntdir);
		}else
		{
			serro("Try mount jffs2 mtd%d to %s return failed [ERROR]", mtdidx, mntdir);
		}
		ret = -1;
	}else
	{
		//挂载命令执行成功
		ret = 0;
	}
	return ret;
}

int create_node_gpio(ual_port_node node)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};

	sprintf(cmdbuf,"echo %s > /sys/class/gpio/export && echo execdone", node.port);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		if(g_lang_zhCN)
		{
			serro("导出IO引脚%s对应的应用节点%s执行失败 [ERROR]", node.port, node.name);
		}else
		{
			serro("Export IO pin %s for node:%s execute failed [ERROR]", node.port, node.name);
		}
		return -1;
	}
	if(strstr(retbuf,"execdone") == NULL)
	{
		if(g_lang_zhCN)
		{
			serro("导出IO引脚%s对应的应用节点%s返回失败 [ERROR]", node.port, node.name);
		}else
		{
			serro("Export IO pin %s for node:%s return failed [ERROR]", node.port, node.name);
		}
		return -1;
	}

	sprintf(cmdbuf,"echo %s > /sys/class/gpio/gpio%s/direction && echo execdone", node.defv, node.port);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		if(g_lang_zhCN)
		{
			serro("为IO引脚%s对应的应用节点%s设置数据流向执行失败 [ERROR]", node.port, node.name);
		}else
		{
			serro("Set direction for IO pin %s for node:%s execute failed [ERROR]", node.port, node.name);
		}
		return -1;
	}
	if(strstr(retbuf,"execdone") == NULL)
	{
		if(g_lang_zhCN)
		{
			serro("为IO引脚%s对应的应用节点%s设置数据流向返回失败 [ERROR]", node.port, node.name);
		}else
		{
			serro("Set direction for IO pin %s for node:%s return failed [ERROR]", node.port, node.name);
		}
		return -1;
	}

	sprintf(cmdbuf,"ln -s /sys/class/gpio/gpio%s /ual/%s && echo execdone", node.port, node.name);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		if(g_lang_zhCN)
		{
			serro("绑定IO引脚%s对应的应用节点%s执行失败 [ERROR]", node.port, node.name);
		}else
		{
			serro("Bound IO pin %s for node:%s execute failed [ERROR]", node.port, node.name);
		}
		return -1;
	}
	if(strstr(retbuf,"execdone") == NULL)
	{
		if(g_lang_zhCN)
		{
			serro("绑定IO引脚%s对应的应用节点%s返回失败 [ERROR]", node.port, node.name);
		}else
		{
			serro("Bound IO pin %s for node:%s return failed [ERROR]", node.port, node.name);
		}
		return -1;
	}
	return 0;
}
int create_node_dlink(ual_port_node node)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};

	sprintf(cmdbuf,"ln -s %s /ual/%s && echo execdone", node.path, node.name);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		if(g_lang_zhCN)
		{
			serro("链接%s到%s执行失败 [ERROR]", node.name, node.path);
		}else
		{
			serro("Link %s to %s execute failed [ERROR]", node.name, node.path);
		}
		return -1;
	}
	if(strstr(retbuf,"execdone") == NULL)
	{
		if(g_lang_zhCN)
		{
			serro("链接%s到%s返回失败 [ERROR]", node.name, node.path);
		}else
		{
			serro("Link %s to %s return failed [ERROR]", node.name, node.path);
		}

		return -1;
	}
	return 0;
}

int exist_eth_class(ual_net_node *node)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};
	sprintf(cmdbuf,"test -e %s/%s && echo exist", NET_CLASS_DIR, node->name);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		return -1;
	}
	if(strstr(retbuf,"exist") == NULL)
	{
		return -1;
	}
	return 0;
}

int exist_eth_sysconfig(ual_net_node *node)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};
	sprintf(cmdbuf,"test -e %s/%s && echo exist", CFG_TOP_DIR, node->name);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		return -1;
	}
	if(strstr(retbuf,"exist") == NULL)
	{
		return -1;
	}
	return 0;
}

int export_node_eth(ual_net_node *node)
{
	int ethidx = 0;

	char cmdbuf[BUF_SIZE_128]={0};

	ethidx = get_node_ethidx(node);
	
	if(g_lang_zhCN)
	{
		sinfo("网口%s的网络策略(proto)  : %s",   node->name, g_netcfg_grp[ethidx].proto);
		sinfo("网口%s的网络地址(ipaddr) : %s",  node->name, g_netcfg_grp[ethidx].ipaddr);
		sinfo("网口%s的子网掩码(netmask): %s", node->name, g_netcfg_grp[ethidx].netmask);
		sinfo("网口%s的网关地址(gateway): %s", node->name, g_netcfg_grp[ethidx].gateway);
	}
	else
	{
		sinfo("%s proto  : %s",   node->name, g_netcfg_grp[ethidx].proto);
		sinfo("%s ipaddr : %s",  node->name, g_netcfg_grp[ethidx].ipaddr);
		sinfo("%s netmask: %s", node->name, g_netcfg_grp[ethidx].netmask);
		sinfo("%s gateway: %s", node->name, g_netcfg_grp[ethidx].gateway);
	}

	sprintf(cmdbuf,"rm -rf %s/%s > /dev/null 2>&1", UAL_TOP_DIR, node->name);
	system(cmdbuf);
	sprintf(cmdbuf,"mkdir -p %s/%s > /dev/null 2>&1", UAL_TOP_DIR, node->name);
	system(cmdbuf);
	sprintf(cmdbuf,"echo %s > %s/%s/proto",   g_netcfg_grp[ethidx].proto,   UAL_TOP_DIR, node->name);
	system(cmdbuf);
	sprintf(cmdbuf,"echo %s > %s/%s/ipaddr",  g_netcfg_grp[ethidx].ipaddr,  UAL_TOP_DIR, node->name);
	system(cmdbuf);
	sprintf(cmdbuf,"echo %s > %s/%s/netmask", g_netcfg_grp[ethidx].netmask, UAL_TOP_DIR, node->name);
	system(cmdbuf);
	sprintf(cmdbuf,"echo %s > %s/%s/gateway", g_netcfg_grp[ethidx].gateway, UAL_TOP_DIR, node->name);
	system(cmdbuf);
	sprintf(cmdbuf,"cat %s/%s/address > %s/%s/macaddr", NET_CLASS_DIR, node->name, UAL_TOP_DIR, node->name);
	system(cmdbuf);
	return 0;
}

int read_eth_netaddr(char *nodename, char *noderoot, char *elemname, char *databuf)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char tmpbuf[BUF_SIZE_128]={0};

	if(databuf == NULL)
	{
		return -1;
	}
	
	sprintf(cmdbuf,"cat %s/%s/%s 2>/dev/null | xargs echo -n", noderoot, nodename, elemname);
	ret = exec_cmd_getret(cmdbuf, databuf, BUF_SIZE_128);
	if(ret != 0 )
	{
		if(g_lang_zhCN)
		{
			serro("从%s/%s/%s读取地址执行失败 [ERROR]", noderoot, nodename, elemname);
		}else
		{
			serro("Read address from %s/%s/%s execute failed [ERROR]", noderoot, nodename, elemname);
		}
		return -1;
	}
	
	if( 0 != utils_ckc_ipvalid(databuf))
	{
		if(g_lang_zhCN)
		{
			serro("从%s/%s/%s读取地址返回失败 [ERROR]", noderoot, nodename, elemname);
		}else
		{
			serro("Read address from %s/%s/%s return failed [ERROR]", noderoot, nodename, elemname);
		}
		return -1;
	}
	return 0;
}

int import_node_eth(ual_net_node *node, char *noderoot)
{
	int ret    = -1;
	int isdhcp = 0;

	int ethidx = 0;
	
	int len_ipaddr  = -1;
	int len_netmask = -1;
	int len_gateway = -1;

	int error_read_address = 0;

	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};

	ual_net_node *tmp_net_node = NULL;

	char * netaddr = NULL;

	ethidx = get_node_ethidx(node);

	if(NULL == noderoot)
	{
		sprintf(g_netcfg_grp[ethidx].proto, "static");
		dmsg("import default: %s proto: %s", node->name, g_netcfg_grp[ethidx].proto);

		len_ipaddr = strlen(node->ipaddr);
		strncpy(g_netcfg_grp[ethidx].ipaddr, node->ipaddr, len_ipaddr);
		dmsg("import default: %s ipaddr: %s, len: %d", node->name,  g_netcfg_grp[ethidx].ipaddr, len_ipaddr);
		
		len_netmask = strlen(node->netmask);
		strncpy(g_netcfg_grp[ethidx].netmask, node->netmask, len_netmask);
		dmsg("import default: %s netmask: %s, len: %d", node->name,  g_netcfg_grp[ethidx].netmask, len_netmask);
		
		len_gateway = strlen(node->gateway);
		strncpy(g_netcfg_grp[ethidx].gateway, node->gateway, len_gateway);
		dmsg("import default: %s gateway: %s, len: %d", node->name,  g_netcfg_grp[ethidx].gateway, len_gateway);
		return 0;
	}

	sprintf(cmdbuf, "test -e %s/%s/proto && cat %s/%s/proto", noderoot, node->name, noderoot, node->name);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		if(g_lang_zhCN)
		{
			serro("读取网口%s的网络策略(proto),文件为%s/%s/proto执行失败 [ERROR]", node->name, noderoot, node->name);
		}else
		{
			serro("Read %s proto from %s/%s/proto execute failed [ERROR]", node->name, noderoot, node->name);
		}
		return -1;
	}
	memset(g_netcfg_grp[ethidx].proto, 0, IP_BUF_SIZE);

	if(strstr(retbuf,"static") != NULL)
	{
		sprintf(g_netcfg_grp[ethidx].proto, "static");
	}
	else if(strstr(retbuf,"dhcp") != NULL)
	{
		sprintf(g_netcfg_grp[ethidx].proto, "dhcp");
	}
	else
	{
		sprintf(g_netcfg_grp[ethidx].proto, "static");
	}

	memset(g_netcfg_grp[ethidx].ipaddr, 0, IP_BUF_SIZE);
	ret = read_eth_netaddr(node->name, noderoot, "ipaddr", retbuf);
	if(ret != 0)
	{
		len_ipaddr = strlen(node->ipaddr);
		strncpy(g_netcfg_grp[ethidx].ipaddr, node->ipaddr, len_ipaddr);
		dmsg("import default: %s ipaddr: %s, len: %d", node->name,  g_netcfg_grp[ethidx].ipaddr, len_ipaddr);
	}
	else
	{
		len_ipaddr = strlen(retbuf);
		strncpy(g_netcfg_grp[ethidx].ipaddr, retbuf, len_ipaddr);
		dmsg("import newconf: %s ipaddr: %s, len: %d", node->name,  g_netcfg_grp[ethidx].ipaddr, len_ipaddr);
	}

	memset(g_netcfg_grp[ethidx].netmask, 0, IP_BUF_SIZE);
	ret = read_eth_netaddr(node->name, noderoot, "netmask", retbuf);
	if(ret != 0)
	{
		len_netmask = strlen(node->netmask);
		strncpy(g_netcfg_grp[ethidx].netmask, node->netmask, len_netmask);
		dmsg("import default: %s netmask: %s, len: %d", node->name,  g_netcfg_grp[ethidx].netmask, len_netmask);
	}
	else
	{
		len_netmask = strlen(retbuf);
		strncpy(g_netcfg_grp[ethidx].netmask, retbuf, len_netmask);
		dmsg("import newconf: %s netmask: %s, len: %d", node->name,  g_netcfg_grp[ethidx].netmask, len_netmask);
	}

	memset(g_netcfg_grp[ethidx].gateway, 0, IP_BUF_SIZE);
	ret = read_eth_netaddr(node->name, noderoot, "gateway", retbuf);
	if(ret != 0)
	{
		len_gateway = strlen(node->gateway);
		strncpy(g_netcfg_grp[ethidx].gateway, node->gateway, len_gateway);
		dmsg("import default: %s gateway: %s, len: %d", node->name,  g_netcfg_grp[ethidx].gateway, len_gateway);
	}
	else
	{
		len_gateway = strlen(retbuf);
		strncpy(g_netcfg_grp[ethidx].gateway, retbuf, len_gateway);
		dmsg("import newconf: %s gateway: %s, len: %d", node->name,  g_netcfg_grp[ethidx].gateway, len_gateway);
	}
	return 0;
}

int config_node_eth_mac(ual_net_node *node)
{
	int ret    = 0;
	int ethidx = 0;
	
	ethidx = get_node_ethidx(node);
	
#if defined (I2C_SYSROM_PATH)
	ret = set_mac_from_psn(node->name, ethidx);
	if(0 != ret)
	{
		ret = set_mac_default(node->name, (unsigned char) ret);
	}
#else
	ret = set_mac_default(node->name, (unsigned char) ret);
#endif

	return ret;
}

int config_node_eth_static(ual_net_node *node)
{
	int ethidx = 0;

	char cmdbuf[BUF_SIZE_128]={0};

	ethidx = get_node_ethidx(node);

	sprintf(cmdbuf,"ifconfig %s down", node->name);
	system(cmdbuf);

	utils_set_netinfo(node->name, g_netcfg_grp[ethidx].ipaddr, g_netcfg_grp[ethidx].netmask, g_netcfg_grp[ethidx].gateway);
	
	sprintf(cmdbuf,"ifconfig %s up", node->name);
	system(cmdbuf);
	
	return 0;
}

int config_node_eth_dhcp(ual_net_node *node)
{
	int ethidx = 0;

	char cmdbuf[BUF_SIZE_128]={0};

	ethidx = get_node_ethidx(node);

	if(0 == strcmp(g_netcfg_grp[ethidx].proto, "dhcp"))
	{		
		sprintf(cmdbuf,"udhcpc -b -i %s -p /var/run/udhcpc.pid -R -T 1 -t 2 -A 10 > /dev/null 2>&1 &", node->name);
		system(cmdbuf);
	}
}

int create_node_eth(ual_net_node *node)
{
	int ret = -1;

	/*check ethernet class is exist*/
	if(0 == exist_eth_class(node))
	{
		if(0 == exist_eth_sysconfig(node))
		{
			dmsg("get %s class sys config exist!",node->name);
			ret = import_node_eth(node, CFG_TOP_DIR);
		}else
		{
			dmsg("get %s class default config exist!",node->name);
			ret = import_node_eth(node, NULL);
		}

		/*config mac addr*/
		config_node_eth_mac(node);
		
		/*config static as default*/
		config_node_eth_static(node);
		
		/*config for dhcp*/
		config_node_eth_dhcp(node);

		/*export node to ual*/
		export_node_eth(node);
	}
	return 0;
}

int exist_dns_sysconfig(ual_net_node *node)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};
	sprintf(cmdbuf,"test -e %s/%s && echo exist", CFG_TOP_DIR, node->name);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		return -1;
	}
	if(strstr(retbuf,"exist") == NULL)
	{
		return -1;
	}
	return 0;
}

int export_node_dns(ual_net_node *node)
{
	char cmdbuf[BUF_SIZE_128]={0};
	//fprintf(stdout, "%s ipaddr: %s\n", node->name, node->ipaddr);
	sprintf(cmdbuf,"rm -rf %s/%s > /dev/null 2>&1", UAL_TOP_DIR, node->name);
	system(cmdbuf);
	sprintf(cmdbuf,"mkdir -p %s/%s > /dev/null 2>&1", UAL_TOP_DIR, node->name);
	system(cmdbuf);
	sprintf(cmdbuf,"echo %s > %s/%s/ipaddr", node->ipaddr, UAL_TOP_DIR, node->name);
	system(cmdbuf);
	return 0;
}

int import_node_dns(ual_net_node *node, char *noderoot)
{
	int ret    = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};

	ual_net_node *tmp_net_node = NULL;

	char * netaddr = NULL;

	tmp_net_node = (ual_net_node *) malloc(sizeof(ual_net_node));
	if(tmp_net_node == NULL)
	{
		return -1;
	}

	tmp_net_node->name = node->name;

	memset(retbuf,0,BUF_SIZE_128);
	ret = read_eth_netaddr(tmp_net_node->name, noderoot, "ipaddr", retbuf);
	if(ret != 0)
	{
		dmsg("import %s ipaddr failed!", tmp_net_node->name);
		free(tmp_net_node);
		return -1;
	}

	tmp_net_node->ipaddr = (char *) malloc( sizeof(char) * strlen(retbuf));
	strcpy(tmp_net_node->ipaddr, retbuf);
	dmsg("import %s ipaddr: %s", tmp_net_node->name, tmp_net_node->ipaddr);

	/*copy data*/
	node->ipaddr = (char *) malloc( sizeof(char) * strlen(tmp_net_node->ipaddr));
	strcpy(node->ipaddr, tmp_net_node->ipaddr);
	dmsg("sync %s ipaddr: %s", node->name, node->ipaddr);

	return 0;
}

int config_node_dns(ual_net_node *node, int isfirst)
{
	char cmdbuf[BUF_SIZE_128]={0};
	if(isfirst == 1)
	{
		/*first dns need overwrite resolve.conf*/
		sprintf(cmdbuf,"echo \"nameserver %s\" > /etc/resolv.conf", node->ipaddr);
	}else
	{
		sprintf(cmdbuf,"echo \"nameserver %s\" >> /etc/resolv.conf", node->ipaddr);
	}
	system(cmdbuf);
	return 0;
}

static int isfirstdns = 1;
int create_node_dns(ual_net_node *node)
{
	int ret = -1;
	//只在CFG_TOP_DIR中查找DNS配置文件
	if(0 == exist_dns_sysconfig(node))
	{
		ret = import_node_dns(node, CFG_TOP_DIR);
	}
	config_node_dns(node, isfirstdns);
	if(isfirstdns == 1)
	{
		isfirstdns = 0;
	}
	export_node_dns(node);
	return 0;
}

//查找配置目录是否存在,其中对appsrv进行特殊处理
int check_srv_cfgdir(ual_srv_node *service)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};
	sprintf(cmdbuf,"test -e %s/%s && echo exist", CFG_TOP_DIR, service->name);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		return -1;
	}
	if(strstr(retbuf,"exist") == NULL)
	{
		return -1;
	}
	return 0;
}

/*更新UAL层指定服务的状态*/
int update_srv_ualsta(ual_srv_node *service)
{
	char cmdbuf[BUF_SIZE_128]={0};

	sprintf(cmdbuf,"rm -rf %s/%s > /dev/null 2>&1", UAL_TOP_DIR, service->name);
	system(cmdbuf);
	sprintf(cmdbuf,"mkdir -p %s/%s > /dev/null 2>&1", UAL_TOP_DIR, service->name);
	system(cmdbuf);
	sprintf(cmdbuf,"echo %s > %s/%s/enable", service->enable, UAL_TOP_DIR, service->name);
	system(cmdbuf);
	sprintf(cmdbuf,"echo %s > %s/%s/path", service->path, UAL_TOP_DIR, service->name);
	system(cmdbuf);
	return 0;
}

int check_srv_enable(ual_srv_node *service, char *cfgdir)
{
	int ret	= -1;

	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};

	//创建临时服务
	ual_srv_node *tmpsrv = NULL;
	tmpsrv = (ual_srv_node *) malloc(sizeof(ual_srv_node));
	if(tmpsrv == NULL)
	{
		return -1;
	}
	tmpsrv->name = service->name;

	//检查配置目录中的使能开关，例如/root/ftpsrv/enable
	sprintf(cmdbuf,"test -e %s/%s/enable && cat %s/%s/enable", cfgdir, service->name, cfgdir, service->name);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0 )
	{
		//查找使能开关enable文件的流程异常
		if(g_lang_zhCN)
		{
			serro("读取%s服务的使能文件%s/%s/enable错误 [ERROR]", service->name, cfgdir, service->name);
		}
		else
		{
			serro("Read service %s file %s/%s/enable failed [ERROR]", service->name, cfgdir, service->name);
		}
		free(tmpsrv);
		//退出时service->enable的值为编译时的默认值
		return -1;
	}
	else
	{
		if(strstr(retbuf,"yes") != NULL)
		{
			//读取到的使能开关enable文件内容为yes，标记使能该服务
			tmpsrv->enable = "yes";
		
		}else if(strstr(retbuf,"no") != NULL)
		{
			//读取到的使能开关enable文件内容为no，标记关闭该服务
			tmpsrv->enable = "no";
		}
		else
		{
			//如果enable文件不存在或者说内容不是yes/no
			//但是服务名称是appsrv，而且路径相同时
			if ( (strstr(service->name,"appsrv") != NULL) && ( strstr(cfgdir, CFG_TOP_DIR) != NULL) )
			{
				//标记使能该服务
				tmpsrv->enable = "yes";
			}else
			{
				tmpsrv->enable = "no";
			}
		}
		//重新整理内存并拷贝临时节点
		service->enable = (char *) malloc( sizeof(char) * strlen(tmpsrv->enable));
		strcpy(service->enable, tmpsrv->enable);
	}

#if 1
	//当服务类型为appsrv且被使能时
	if( (strstr(service->name,"appsrv") != NULL) && (strstr(tmpsrv->enable, "yes") != NULL) )
	{
		//查找文件是否存在，例如/root/syapp
		sprintf(cmdbuf,"test -e %s && echo exist", service->path);
		ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
		if((ret == 0) && (strstr(retbuf,"exist") != NULL))
		{
			//已检测到应用程序
			//修改应用程序为可执行
			sprintf(cmdbuf,"chmod 777 %s > /dev/null 2>&1", service->path);
			system(cmdbuf);			
			//注意此时service->path保存着srv_config中定义的默认启动程序的路径
		}else
		{
			//未检测到应用程序
			//设置服务应用程序路径为空
			tmpsrv->path = "none";
			//重新设置服务应用程序路径
			service->path = (char *) malloc( sizeof(char) * strlen(tmpsrv->path));
			strcpy(service->path, tmpsrv->path);
		}
	}
#else
	//当服务类型为appsrv且被使能时
	if( (strstr(service->name,"appsrv") != NULL) && (strstr(tmpsrv->enable, "yes") != NULL) )
	{
		//查找文件是否存在，例如/root/syapp
		sprintf(cmdbuf,"test -e %s/%s && echo exist", APP_TOP_DIR, APP_ELF_NAME);
		ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
		if((ret == 0) && (strstr(retbuf,"exist") != NULL))
		{
			//已检测到应用程序
			dmsg("check %s sysapp: %s/%s exist yes!\n", service->name, APP_TOP_DIR, APP_ELF_NAME);
			//修改应用程序为可执行
			sprintf(cmdbuf,"chmod 777 %s/%s > /dev/null 2>&1", APP_TOP_DIR, APP_ELF_NAME);
			system(cmdbuf);			
			//注意此时service->path保存着srv_config中定义的默认启动程序的路径
		}else
		{
			//未检测到应用程序
			dmsg("check %s sysapp: %s/%s exist no!\n", service->name, APP_TOP_DIR, APP_ELF_NAME);

			//设置服务应用程序路径为空
			tmpsrv->path = "none";

			//重新设置服务应用程序路径
			service->path = (char *) malloc( sizeof(char) * strlen(tmpsrv->path));
			strcpy(service->path, tmpsrv->path);
			dmsg("sync %s path: %s\n", service->name, service->path);
		}
	}
#endif
	free(tmpsrv);
	return 0;
}

//启动服务的应用程序
int start_srv_app(ual_srv_node *service)
{
	char cmdbuf[BUF_SIZE_128]={0};

	if(strstr(service->enable, "yes") != NULL)
	{
		if(strstr(service->name, "sshsrv") != NULL)
		{
			system("killall -s 9 dropbear > /dev/null 2>&1");
			system("dropbear &");

			if(g_lang_zhCN)
			{
				sinfo("安全登录服务(SSH)   : 已开启");
			}else
			{
				sinfo("Use system SSH service    : YES");
			}
		}
		else if(strstr(service->name, "ftpsrv") != NULL)
		{
		   system("killall -s 9 vsftpd > /dev/null 2>&1");
		   system("vsftpd &");

			if(g_lang_zhCN)
			{
				sinfo("文件传输服务(TFTP)  : 已开启");
			}else
			{
				sinfo("Use system TFTP service   : YES");
			}
		}
		else if(strstr(service->name, "telnetsrv") != NULL)
		{
			system("killall -s 9 telnetd > /dev/null 2>&1");
			system("telnetd &");

			if(g_lang_zhCN)
			{
				sinfo("远程终端服务(TELNET): 已开启");
			}else
			{
				sinfo("Use system TELNET service : YES");
			}
		}
		else if(strstr(service->name, "appsrv") != NULL)
		{
			if(strstr(service->path, "none") != NULL )
			{
				if(g_lang_zhCN)
				{
					serro("自动执行开机默认程序: 默认程序未找到");
				}else
				{
					serro("Need autorun default app  : no default app found");
				}
			}
			else
			{
				if(g_lang_zhCN)
				{
					sinfo("自动执行开机默认程序: 已设置");
					sinfo("开机默认程序绝对路径: %s", service->path);
					sinfo("开机默认程序参入参数: %s", service->args);
				}else
				{
					sinfo("Default app path: %s", service->path);
					sinfo("Default app args: %s", service->args);
				}
				system("killall -s 9 sysapp > /dev/null 2>&1");
				if(0 == strcmp(service->args, "none"))
				{
					sprintf(cmdbuf,"%s > /dev/null 2>&1 &", service->path);
				}else
				{
					sprintf(cmdbuf,"%s %s > /dev/null 2>&1 &", service->path, service->args);
				}
				system(cmdbuf);
			}
		}
	}else
	{
		if(g_lang_zhCN)
		{
			sinfo("是否开启%s服务? 未开启", service->name);
		}else
		{
			sinfo("Enable service %s? NO", service->name);
		}
	}
	return 0;
}

int create_node_srv(ual_srv_node *service)
{
	int ret = -1;
	//查找配置目录是否存在,其中对appsrv进行特殊处理
	if((0 == check_srv_cfgdir(service)) || (0 == strcmp(service->name, "appsrv")))
	{
		//检查是否启用服务
		ret = check_srv_enable(service, CFG_TOP_DIR);
	}
	//启动服务的应用程序
	start_srv_app(service);
	//更新服务在UAL层中的状态
	update_srv_ualsta(service);
	return 0;
}

int setup_system(void)
{
	char cmdbuf[BUF_SIZE_32]={0};

	//创建UAL顶层目录
	sprintf (cmdbuf, "mkdir -p %s", UAL_TOP_DIR);
	system (cmdbuf);

	//挂载UAL顶层目录为临时文件系统
	sprintf(cmdbuf,"mount -t tmpfs tmpfs %s",UAL_TOP_DIR);
	system (cmdbuf);
	return 0;
}


//UBI分区结构体
typedef struct disk_part_node_t
{
	//UBI分区编号,-1表示不分区
	int ubi_idx;
	//UBI分区对应的MTD分区索引
	int mtd_idx; 
	//UBI分区对应的MTD分区名称
	char *mtd_name;
	//UBI分区对应的挂载目录
	char *mnt_dir;

} disk_part_node;

//磁盘分区描述符表,把所有MTD分区都看成磁盘分区对应的节点
static disk_part_node disk_part_node_list[]=
{
#ifdef RTFS_MTDSUB_BOOT
#if RTFS_MTDSUB_BOOT
	{
		/*BOOT不分配UBI索引*/
		.ubi_idx  = -1,
#ifdef RTFS_MTDIDX_BOOT
		.mtd_idx  = (RTFS_MTDIDX_BOOT),
#else
		.mtd_idx  = -1,
#endif
		.mtd_name = "boot",
		.mnt_dir  = NULL,
	},
#endif
#endif //#ifdef RTFS_MTDSUB_BOOT

#ifdef RTFS_MTDSUB_KNFS
#if RTFS_MTDSUB_KNFS
	{
		/*KNFS不分配UBI索引*/
		.ubi_idx  = -1,
#ifdef RTFS_MTDIDX_KNFS
		.mtd_idx  = RTFS_MTDIDX_KNFS,
#else
		.mtd_idx  = -1,
#endif
		.mtd_name = "knfs",
		.mnt_dir  = NULL,
	},
#endif
#endif //#ifdef RTFS_MTDSUB_KNFS


/**************************************
 * RTFS不在rc.local管理范围内所以没有配置
 **************************************/

/***************************************/
/*RTFS以后的分区UBI分区必须从ubi1_0算起*/
#ifdef RTFS_MTDSUB_OVFS
#if    RTFS_MTDSUB_OVFS
	{
#ifdef RTFS_UBIIDX_OVFS
		.ubi_idx  = RTFS_UBIIDX_OVFS,
#else
		.ubi_idx  = -1,
#endif
#ifdef RTFS_MTDIDX_OVFS
		.mtd_idx  = RTFS_MTDIDX_OVFS,
#else
		.mtd_idx  = -1,
#endif
		.mtd_name = "ovfs",
		.mnt_dir  = "/ovfs",
	},
#endif
#endif

#ifdef RTFS_MTDSUB_EXTD
#if    RTFS_MTDSUB_EXTD
	{
#ifdef RTFS_UBIIDX_EXTD
		.ubi_idx  = RTFS_UBIIDX_EXTD,
#else
		.ubi_idx  = 1,
#endif
#ifdef RTFS_MTDIDX_EXTD
		.mtd_idx  = RTFS_MTDIDX_EXTD,
#else
		.mtd_idx  = -1,
#endif
		.mtd_name = "extd",
		.mnt_dir  = "/extd",
	},
#endif
#endif

#ifdef RTFS_MTDSUB_ROOT
#if    RTFS_MTDSUB_ROOT
	{
#ifdef RTFS_UBIIDX_ROOT
		.ubi_idx  = RTFS_UBIIDX_ROOT,
#else
		.ubi_idx  = -1,
#endif
#ifdef RTFS_MTDIDX_ROOT
		.mtd_idx  = RTFS_MTDIDX_ROOT,
#else
		.mtd_idx  = -1,
#endif
		.mtd_name = "root",
		.mnt_dir  = "/root",
	},
#endif
#endif

#ifdef RTFS_MTDSUB_DATA
#if    RTFS_MTDSUB_DATA
	{
#ifdef RTFS_UBIIDX_DATA
		.ubi_idx  = RTFS_UBIIDX_DATA,
#else
		.ubi_idx  = -1,
#endif
#ifdef RTFS_MTDIDX_DATA
		.mtd_idx  = RTFS_MTDIDX_DATA,
#else
		.mtd_idx  = -1,
#endif
		.mtd_name = "data",
		.mnt_dir  = "/data",
	},
#endif
#endif

#ifdef RTFS_MTDSUB_VEND
#if    RTFS_MTDSUB_VEND
	{
#ifdef RTFS_UBIIDX_VEND
		.ubi_idx  = RTFS_UBIIDX_VEND,
#else
		.ubi_idx  = -1,
#endif
#ifdef RTFS_MTDIDX_VEND
		.mtd_idx  = RTFS_MTDIDX_VEND,
#else
		.mtd_idx  = -1,
#endif
		.mtd_name = "vend",
		.mnt_dir  = "/vend",
	},
#endif
#endif
};

//磁盘分区总数
#define disk_part_total (sizeof(disk_part_node_list) / sizeof(disk_part_node_list[0]))

//设置磁盘分区
int setup_diskpart(void)
{
#if ( defined (RTFS_MTDFSTYP_UBIFS) || defined (RTFS_MTDFSTYP_YAFS2) || (RTFS_MTDFSTYP_JFFS2))
	int ret = 0;
	int try = 3;
	int try_cnt = 0;
	int mtd_idx = 0;
	int tmp_idx = 0;

	char retbuf[BUF_SIZE_128]={0};
	char cmdbuf[BUF_SIZE_128]={0};

	//MTD分区总数
	int mtd_total = 0;

	if(g_lang_zhCN)
	{
		dmsg("从磁盘分区列表获取磁盘分区总数: %d", disk_part_total);
	}
	else
	{
		dmsg("Get disk partiton total from list: %d", disk_part_total);
	}

	//创建新的mtdinfo文件
	sprintf (cmdbuf,"rm -rf %s/%s > /dev/null 2>&1", UAL_TOP_DIR, UAL_MTD_DIR);
	system (cmdbuf);
	sprintf (cmdbuf,"mkdir -p %s/%s > /dev/null 2>&1", UAL_TOP_DIR, UAL_MTD_DIR);
	system (cmdbuf);

	//获取MTD分区总数
	ret = exec_cmd_getret("cat /proc/mtd | grep mtd | wc -l", retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		if(g_lang_zhCN)
		{
			serro("从运行系统读取MTD分区总数失败 [ERROR]");
		}else
		{
			serro("Get system runtime mtd partition total failed [ERROR]");
		}
		return -1;
	}
	mtd_total = atoi(retbuf);

	if(g_lang_zhCN)
	{
		dmsg("从运行系统读取MTD分区总数: %d", mtd_total);
	}else
	{
		dmsg("Get system runtime mtd partition total: %d", mtd_total);
	}

	//遍历所有找到的MTD分区
	for(mtd_idx = 0; mtd_idx < mtd_total; mtd_idx++)
	{
		sprintf(cmdbuf,"cat /sys/class/mtd/mtd%d/name", mtd_idx);
		ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
		if(ret != 0)
		{
			//出现找到的MTD分区无法读取名字的错误
			if(g_lang_zhCN)
			{
				serro("从运行系统读取MTD分区mtd%d的磁盘分区名称失败 [ERROR]", mtd_idx);
			}else
			{
				serro("Read system runtime mtd%d disk partition name failed [ERROR]", mtd_idx);
			}
			return -1;
		}

		//磁盘分区列表与MTD分区列表进行匹配检测
		for(tmp_idx = 0; tmp_idx < disk_part_total; tmp_idx++)
		{
			//因为retbuf包含换行符不能用strcmp
			if(strstr(retbuf, disk_part_node_list[tmp_idx].mtd_name) != NULL)
			{
			
				if(g_lang_zhCN)
				{
					dmsg("从运行系统中找到磁盘分区%s对应的MTD分区:mtd%d", disk_part_node_list[tmp_idx].mtd_name, mtd_idx);
				}else
				{
					dmsg("Found systm runtime mtd%d for disk partition %s", mtd_idx, disk_part_node_list[tmp_idx].mtd_name);
				}
				
				if(disk_part_node_list[tmp_idx].mtd_idx == -1)
				{
					//记录MTD分区索引
					disk_part_node_list[tmp_idx].mtd_idx = mtd_idx;
					
					if(g_lang_zhCN)
					{
						dmsg("重新设定磁盘分区%s的MTD分区为mtd%d", disk_part_node_list[tmp_idx].mtd_name, mtd_idx);
					}else
					{
						dmsg("Resume disk partition %s to mtd partition mtd%d", disk_part_node_list[tmp_idx].mtd_name, mtd_idx);
					}
				}else
				{
					if(disk_part_node_list[tmp_idx].mtd_idx != mtd_idx)
					{
						if(g_lang_zhCN)
						{
							serro("匹配磁盘分区%s的MTD分区mtd%d与运行系统MTD分区mtd%d失败 [ERROR]", 
									disk_part_node_list[tmp_idx].mtd_name,
									disk_part_node_list[tmp_idx].mtd_idx, mtd_idx);
						}else
						{
							serro("Match disk partition %s mtd%d and system runtime mtd%d failed [ERROR]", 
									disk_part_node_list[tmp_idx].mtd_name,
									disk_part_node_list[tmp_idx].mtd_idx, mtd_idx);
						}
						return -1;
					}else
					{
						if(g_lang_zhCN)
						{
							dmsg("匹配磁盘分区%s的MTD分区mtd%d成功 [OK]", disk_part_node_list[tmp_idx].mtd_name, mtd_idx);
						}else
						{
							dmsg("Match disk partion %s mtd%d finish [OK]", disk_part_node_list[tmp_idx].mtd_name, mtd_idx);
						}
					}
				}
				//跳出本层for循环
				break;
			}
		}
	}
	
	for(tmp_idx = 0; tmp_idx < disk_part_total; tmp_idx++)
	{
		if(disk_part_node_list[tmp_idx].ubi_idx == -1)
		{
			//不需要挂载到指定目录的磁盘分区对应的MTD分区直接跳过
			continue;
		}

		if(disk_part_node_list[tmp_idx].mtd_idx == -1)
		{
			//磁盘分区的MTD分区没有被找到
			if(g_lang_zhCN)
			{
				dmsg("没有为磁盘分区%s设定MTD分区，跳过挂载设置",  disk_part_node_list[tmp_idx].mtd_name);
			}else
			{
				dmsg("No mtd partition asumed for disk partition %s, skip mount setting",  disk_part_node_list[tmp_idx].mtd_name);
			}
			continue;
		}
		if(disk_part_node_list[tmp_idx].mnt_dir == NULL)
		{
			//磁盘分区的的挂载目录没有指定
			if(g_lang_zhCN)
			{
				dmsg("没有为磁盘分区%s指定挂载目录，跳过挂载设置",  disk_part_node_list[tmp_idx].mtd_name);
			}else
			{
				dmsg("No mount directory asumed for disk partition %s, skip mount setting",  disk_part_node_list[tmp_idx].mtd_name);
			}
			continue;
		}
		for(try_cnt = 0; try_cnt < try; try_cnt++)
		{
#if defined (RTFS_MTDFSTYP_UBIFS)
			//挂载指定MTD分区到指定磁盘分区对应的UBI节点
			ret = ubifs_mnt(
					disk_part_node_list[tmp_idx].mtd_idx,
					disk_part_node_list[tmp_idx].ubi_idx,
					disk_part_node_list[tmp_idx].mnt_dir,
					1);
			if(0 != ret)
			{
				if(g_lang_zhCN)
				{
					dmsg("无法将磁盘分区%s以ubifs格式挂载到目录%s, 开始尝试修复", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}else
				{
					dmsg("Failed mount ubifs disk partition %s to mount directory %s, start fix", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}
				//没有挂载成功需要进行修复
				ubifs_fix(
					disk_part_node_list[tmp_idx].mtd_idx,
					disk_part_node_list[tmp_idx].ubi_idx,
					disk_part_node_list[tmp_idx].mtd_name);
			}else
			{
				if(g_lang_zhCN)
				{
					dmsg("成功将磁盘分区%s以ubifs格式挂载到目录%s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}else
				{
					dmsg("finish mount ubifs disk partition %s to mount directory %s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}
				//跳过最近的for循环
				break;
			}
#elif defined  (RTFS_MTDFSTYP_JFFS2)
			//挂载指定MTD分区到指定磁盘分区对应的目录
			ret = jffs2_mnt(
					disk_part_node_list[tmp_idx].mtd_idx,
					disk_part_node_list[tmp_idx].mnt_dir);
			if(0 != ret)
			{
				if(g_lang_zhCN)
				{
					dmsg("无法将磁盘分区%s以jffs2格式挂载到目录%s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}else
				{
					dmsg("Failed mount jffs2 disk partition %s to mount directory %s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}
			}else
			{
				if(g_lang_zhCN)
				{
					dmsg("成功将磁盘分区%s以jffs2格式挂载到目录%s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}else
				{
					dmsg("finish mount jffs2 disk partition %s to mount directory %s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}
				//跳过最近的for循环
				break;
			}
#else
			//挂载指定MTD分区到指定磁盘分区对应的目录
			ret = yaffs2_mnt(
					disk_part_node_list[tmp_idx].mtd_idx,
					disk_part_node_list[tmp_idx].mnt_dir);
			if(0 != ret)
			{
				if(g_lang_zhCN)
				{
					dmsg("无法将磁盘分区%s以yaffs2格式挂载到目录%s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}else
				{
					dmsg("Failed mount yaffs2 disk partition %s to mount directory %s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}
			}else
			{
				if(g_lang_zhCN)
				{
					dmsg("成功将磁盘分区%s以yaffs2格式挂载到目录%s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}else
				{
					dmsg("finish mount yaffs2 disk partition %s to mount directory %s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
				}
				//跳过最近的for循环
				break;
			}
#endif
		} //for(try_cnt = 0; try_cnt < try; try_cnt++)

		if(try_cnt >= try)
		{
			//挂载失败
			if(g_lang_zhCN)
			{
				serro("经过多次尝试，无法将磁盘分区%s挂载到目录%s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);

			}else
			{
				serro("Finally, failed mount disk partition %s to mount directory %s", disk_part_node_list[tmp_idx].mtd_name, disk_part_node_list[tmp_idx].mnt_dir);
			}
		}//if(try_cnt >= try)
	}//for(tmp_idx = 0; tmp_idx < disk_part_total; tmp_idx++)
	return 0;
#else  //#if ( defined (RTFS_MTDFSTYP_UBIFS) || defined (RTFS_MTDFSTYP_YAFS2) || (RTFS_MTDFSTYP_JFFS2))
	return 0;
#endif
}

int setup_port(void)
{
	int idx = 0;
	int max = 0;

	max = sizeof(ual_port_node_list)/sizeof(ual_port_node_list[0]);

	if(g_lang_zhCN)
	{
		dmsg("IO控制引脚总数: %d", max);
	}else
	{
		dmsg("IO control pin total: %d", max);
	}

	for(idx = 0; idx < max; idx++)
	{
		if(g_lang_zhCN)
		{
			dmsg("开始设置IO控制引脚: %s", ual_port_node_list[idx].name);
		}else
		{
			dmsg("Setup IO control pin: %s", ual_port_node_list[idx].name);
		}

		if((ual_port_node_list[idx].type == dout) || (ual_port_node_list[idx].type == din))
		{
			create_node_gpio(ual_port_node_list[idx]);
		}
		else if(ual_port_node_list[idx].type == dlink)
		{
			create_node_dlink(ual_port_node_list[idx]);
		}
	}
	return 0;
}

int setup_network(void)
{
	int idx = 0;
	int max = 0;

	char cmdbuf[BUF_SIZE_128]={0};

	sprintf(cmdbuf,"ifconfig lo down");
	system(cmdbuf);
	sprintf(cmdbuf,"ifconfig lo 127.0.0.1");
	system(cmdbuf);
	sprintf(cmdbuf,"ifconfig lo up");
	system(cmdbuf);

	max = sizeof(ual_net_node_list)/sizeof(ual_net_node_list[0]);

	if(g_lang_zhCN)
	{
		dmsg("网络接口总数: %d", max);
	}else
	{
		dmsg("Network interface total: %d", max);
	}

	for(idx = 0; idx < max; idx++)
	{
		if(g_lang_zhCN)
		{
			dmsg("开始设置网络接口: %s", ual_net_node_list[idx].name);
		}else
		{
			dmsg("Setup network: %s", ual_net_node_list[idx].name);
		}

		if(ual_net_node_list[idx].type == eth)
		{
			create_node_eth(&ual_net_node_list[idx]);
		}
		else if(ual_net_node_list[idx].type == dns)
		{
			create_node_dns(&ual_net_node_list[idx]);
		}
	}
	return 0;
}

int setup_srv(void)
{
	int idx = 0;
	int max = 0;

	char cmdbuf[BUF_SIZE_128]={0};

	max = sizeof(ual_srv_node_list)/sizeof(ual_srv_node_list[0]);

	if(g_lang_zhCN)
	{
		dmsg("当前服务总数: %d", max);
	}else
	{
		dmsg("Current service total: %d", max);
	}

	for(idx = 0; idx < max; idx++)
	{
		if(g_lang_zhCN)
		{
			dmsg("开始设置服务: %s", ual_srv_node_list[idx].name);
		}else
		{
			dmsg("Setup service: %s", ual_srv_node_list[idx].name);
		}

		if(ual_srv_node_list[idx].type == srv)
		{
			create_node_srv(&ual_srv_node_list[idx]);
		}
	}
	return 0;
}

int check_file_exist(char *filename)
{
	int ret = -1;
	char cmdbuf[BUF_SIZE_128]={0};
	char retbuf[BUF_SIZE_128]={0};
	sprintf(cmdbuf,"test -e %s && echo exist", filename);
	ret = exec_cmd_getret(cmdbuf, retbuf, BUF_SIZE_128);
	if(ret != 0)
	{
		return FALSE;
	}
	if(strstr(retbuf,"exist") == NULL)
	{
		return FALSE;
	}
	return TRUE;
}

int main(void)
{
	int  ret = 0;
	char *debug_show_str = NULL;
	char *lang_zhCN_str  = NULL;

	pinfo("====================================================");

	if ((debug_show_str = getenv("RCSKIT_DEBUG")) != NULL) 
	{
		ret = atoi(debug_show_str);
		if(1 == ret)
		{
			g_debug_show = 1;
		}else
		{
			g_debug_show = 0;
		}
	}else
	{
		g_debug_show = 0;
	}

	if ((lang_zhCN_str = getenv("LANG")) != NULL) 
	{
		if(strstr(lang_zhCN_str, "zh_CN") != NULL)
		{
			g_lang_zhCN = 1;
		}else
		{
			g_lang_zhCN = 0;
		}
	}else
	{
		g_lang_zhCN = 0;
	}

	if(g_lang_zhCN)
	{
		sinfo("RTFS构建版本号: %s", RTFS_BUILD_VER_STR);
	}else
	{
		sinfo("RTFS Build Version: %s", RTFS_BUILD_VER_STR);
	}

	if(0 != setup_system())
	{
		if(g_lang_zhCN)
		{
			serro("系统初始化设置失败 [ERROR]");
		}
		else
		{
			serro("Setup system basic setting failed) [ERROR]");
		}
	}

	/*设置磁盘分区*/
	if(0 != setup_diskpart())
	{
		if(g_lang_zhCN)
		{
			serro("系统存储介质设置失败 [ERROR]");
		}
		else
		{
			serro("(Setup system storage failed) [ERROR]");
		}
	}

	setup_sysver();

	if(0 != setup_port())
	{
		if(g_lang_zhCN)
		{
			serro("系统外围接口设置失败 [ERROR]");
		}
		else
		{
			serro("(Setup system interface failed) [ERROR]");
		}
	}

#if defined (I2C_SYSROM_PATH)
	setup_psn();
#endif

	if(0 != setup_network())
	{
		if(g_lang_zhCN)
		{
			serro("系统网络接口设置失败 [ERROR]");
		}
		else
		{
			serro("(Setup system network failed) [ERROR]");
		}
	}

	if(0 != setup_srv())
	{
		if(g_lang_zhCN)
		{
			serro("系统设置默认服务失败 [ERROR]");
		}
		else
		{
			serro("(Setup default service failed) [ERROR]");
		}
	}
	pinfo("====================================================");
}
